package com.jstyle.jclifexd.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.utils.ScreenUtils;

/**
 * Created by Administrator on 2018/9/18.
 */

public class TrackStopView extends android.support.v7.widget.AppCompatButton  {

    private int count = 0;
    private float SweepAngel = 0;
    private RectF rectF;
    private Paint bgCiclePaint;
    private int strokeWidth;
    private int defaultHeight;
    private int defaultWidth;
    private Bitmap bitmap;
    private AnimatorSet animatorSet;

    public TrackStopView(Context context) {
        super(context);
    }

    public TrackStopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.TrackStopView);
        int id=array.getResourceId(R.styleable.TrackStopView_bitmap,R.drawable.exercise_finish);
        bitmap= BitmapFactory.decodeResource(getResources(), id);
        final GestureDetector gestureDetector=new GestureDetector(context,new MyGestureDetector());
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_UP:
                        cancel();
                        break;
                }
                return gestureDetector.onTouchEvent(event);
            }
        });
        init();
    }

    private void init() {


        defaultHeight = ScreenUtils.dip2px(getContext(), 50);
        defaultWidth = ScreenUtils.dip2px(getContext(), 50);
        rectF = new RectF();
        strokeWidth = ScreenUtils.dip2px(getContext(), 6);
        bgCiclePaint = new Paint();
        bgCiclePaint.setColor(Color.GREEN);
        bgCiclePaint.setAntiAlias(true);
        bgCiclePaint.setStyle(Paint.Style.STROKE);
        bgCiclePaint.setStrokeWidth(strokeWidth);
        bgCiclePaint.setStrokeCap(Paint.Cap.ROUND);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(isFinish)return;
        rectF.set(0+strokeWidth/2, 0+strokeWidth/2, getWidth()-strokeWidth/2, getHeight()-strokeWidth/2);
        canvas.drawBitmap(bitmap,getWidth()/2-bitmap.getWidth()/2,getHeight()/2-bitmap.getHeight()/2,bgCiclePaint);
        drawBgArc(canvas);
    }

    public void setBitmap(int id){
        this.bitmap=BitmapFactory.decodeResource(getResources(), id);;
        postInvalidate();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
            height = bitmap.getHeight()+2*strokeWidth;
        }
        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
            width = bitmap.getWidth()+2*strokeWidth;
        }
        setMeasuredDimension(width, height);
    }

    private void drawBgArc(Canvas canvas) {
        canvas.drawArc(rectF, -90, SweepAngel, false, bgCiclePaint);
    }

    long touchTime = 0;
    boolean isClick=true;



    boolean isFinish;

    private static final String TAG = "TrackStopView";
    public void cancel() {
        Log.i(TAG, "cancel: up");
        if(animatorSet!=null){
            animatorSet.cancel();
        }
    }
    public void setOnStopListener( onLongClickListener clickListener){
        this.finishClickListener=clickListener;
    }
    onLongClickListener finishClickListener;



//    @Override
//    public boolean onTouchEvent( MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                touchTime = System.currentTimeMillis();
//                startAnim();
//                break;
//            case MotionEvent.ACTION_UP:
//            case MotionEvent.ACTION_CANCEL:
//                if(System.currentTimeMillis()-touchTime>100){
//                    isClick=true;
//                }else{
//                    isClick=false;
//                }
//                cancel();
//                break;
//        }
//        return isClick;
//
//    }

    public interface onLongClickListener{
        public void finish();
        public void onClick();
    }
    public void setProgress(float progress){
        SweepAngel = 360*progress;
        postInvalidate();
    }
    boolean isCancel;
    public void startAnim() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "progress",
                0, 1);
        if(animatorSet==null){
            animatorSet = new AnimatorSet();
            animatorSet.setDuration(2000);
            animatorSet.setInterpolator(new LinearInterpolator());
            animatorSet.setTarget(this);
            animatorSet.play(animator);
            animatorSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationCancel(Animator animation) {
                    super.onAnimationCancel(animation);
                    SweepAngel=0;
                    Log.i(TAG, "onAnimationCancel: ");
                    isCancel=true;
                    postInvalidate();
                    
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    if(finishClickListener!=null&&!isCancel){
                        Log.i(TAG, "onComplete: f");
                       // cancel();
                        finishClickListener.finish();
                    }
                }

            });
        }
        isCancel=false;
        animatorSet.start();

    }

    class MyGestureDetector implements GestureDetector.OnGestureListener{
        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if(finishClickListener!=null)
                finishClickListener.onClick();
            Log.i(TAG, "onSingleTapUp: click");
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            startAnim();
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    }

}
